package com.talent.web.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.talent.web.feign.wx.WxLoginDto;
import com.talent.web.model.TalentResult;
import com.talent.web.model.dto.LoginDto;
import com.talent.web.model.dto.MobileLoginDto;
import com.talent.web.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @ClassName：LoginController
 * @Author: hszhou
 * @Date: 2024/10/21 13:35
 * @Description: 登录控制器
 */
@RestController
@RequestMapping("login")
public class LoginController {



    @Autowired
    private ILoginService loginService;

    @PostMapping("/admin")
    public TalentResult<String> login(@RequestBody LoginDto loginDto) throws Exception {
        String login = loginService.login(loginDto);
        return TalentResult.success(login);
    }

    @PostMapping("/admin/phone")
    public TalentResult<String> login(@RequestBody MobileLoginDto loginDto) throws Exception {
        String token = loginService.loginByPhone(loginDto);
        return TalentResult.success(token);
    }

    @PostMapping("/app/phone")
    public TalentResult<String> appLoginByPhone(@RequestBody MobileLoginDto loginDto) throws Exception {
        String token = loginService.appLoginByPhone(loginDto);
        return TalentResult.success(token);
    }

    @GetMapping("/wx")
    public TalentResult<String> wxLogin(WxLoginDto loginDto){
        String token = loginService.wxLogin(loginDto);
        return TalentResult.success(token);
    }





    /**
     * 验证码
     */
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response) throws IOException {

        response.setContentType("image/jpeg");
        /*
         * 长度
         * 高度
         * 数字长度
         * 干扰线的数量
         *
         */
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 30, 4, 10);
        String code = circleCaptcha.getCode();

        //保存验证码
        //写出
        circleCaptcha.write(response.getOutputStream());

    }




    @GetMapping("/sms/sendCode")
    public TalentResult<Void> sendSms(String phone){
        loginService.sendSms(phone);
        return TalentResult.success();
    }




    @PostMapping("/logout")
    public TalentResult<HashMap<String,Object>> logout(){
         loginService.logout();
        return TalentResult.success();
    }



}
